package com.lemon.smartsafety.ui.activity;


import android.content.Context;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.event.notice.NoticeRefreshEvent;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.ui.web.BaseWebView;
import com.lemon.smartsafety.ui.web.MyWebChromeClient;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.common.StringUtils;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by lucas on 2017/2/9.
 */

public class EchartsActivity extends SwipeBackActivity {
    @Bind(R.id.webLayout)
    FrameLayout mWebLayout;
    //WebView mWebView;

    String sId = "";
    boolean bHasRead = false;
    @Bind(R.id.baseWebView)
    BaseWebView baseWebView;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            sId = getIntent().getStringExtra(BundleKey.NOTICE_ID);
            bHasRead = getIntent().getBooleanExtra(BundleKey.HAS_READ, false);
        }
        sId = StringUtils.nullValue(sId);

    }

    @Override
    protected void initViews() {
        super.initViews();

        //mWebView = new WebView(getApplicationContext());
       /* mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setJavaScriptEnabled(true);*/


        WebSettings ws = baseWebView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setDomStorageEnabled(true);

        String appCachePath = this.getCacheDir().getAbsolutePath();
        ws.setAppCachePath(appCachePath);
        ws.setDatabaseEnabled(true);
        String dbPath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        ws.setDatabasePath(dbPath);
        ws.setDomStorageEnabled(true);

        ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
        ws.setAllowFileAccess(true);
        ws.setAppCacheEnabled(false);


        baseWebView.setWebChromeClient(new MyWebChromeClient());
        baseWebView.setWebViewClient(new WebViewClient());
        baseWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        baseWebView.setBackgroundColor(0);
        //mWebLayout.addView(mWebView);
        String url = new SharedPreferencesServer(getApplicationContext()).getRequestUrl(UrlInfo.VIEW_ECHARTS);
        //url += "?rp=" + Constants.RP_PASSWORD + "&notice_id=" + sId + "&user_id=" + mSharedPreferencesHelper.getUserId();
        baseWebView.loadUrl(url);
    }

    @Override
    protected void initTitle() {
        setBarTitle("数据显示");
    }
    @Override
    protected int getContentViewId() {
        return R.layout.activity_notice_detail;
    }

    private void sendRefreshEvent() {
        NoticeRefreshEvent event = new NoticeRefreshEvent();
        AppService.getBus().post(event);
    }
    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(EchartsActivity.this);
        if (!bHasRead) {
            sendRefreshEvent();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebLayout != null) {
            mWebLayout.removeAllViews();
            baseWebView.destroy();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.web_view_layout);
//        mWebView = (WebView) findViewById(R.id.see_web_view);
//        WebSettings settings = mWebView.getSettings();
//        Intent it = getIntent();
//        String WebUrl = it.getStringExtra("WebUrl");
//        mWebView.loadUrl(WebUrl);
//
//        settings.setJavaScriptEnabled(true); //这里如果本地html引入了javascript那么需要把这个设置为true
//        settings.setAllowFileAccess(true);
//
//        settings.setDefaultTextEncodingName("utf-8"); //设置文本编码r
//        settings.setAppCacheEnabled(true);
//        settings.setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存模式
//        settings.setDomStorageEnabled(true);
//        settings.setBuiltInZoomControls(true);
//        settings.setSupportZoom(true);
//        settings.setLoadWithOverviewMode(true);
//        settings.setUseWideViewPort(true);
//        settings.setLoadsImagesAutomatically(true);
//
//        mWebView.setWebViewClient(new WebViewClient() {
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });
//    }true
}
